package com.gaogzhen.security.activity;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "c.TestDeadLock01")
public class TestDeadLock01 {
    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (a) {
                log.debug("lock a");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    log.debug("lock b");
                    log.debug("series of operations");
                }
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized (b) {
                log.debug("lock b");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a) {
                    log.debug("lock a");
                    log.debug("series of operations");
                }
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
